document.querySelector("#btn-login").addEventListener("click", async () => {

    //搜集数据
    const form = document.querySelector(".login-form")
    const data = serialize(form, {
        hash: true,
        empty: true
    });
    // console.log(data);{"":""}
    console.log(data)
    const {
        username,
        password
    } = data


    //非空验证
    if (username === '' || password === '') {
        showToast("用户名或密码不能为空")
        return
    }



    //长度校验

    if (username.length < 3 || username.length > 30 || password.length < 3 || password.length > 30) {
        showToast("用户名或密码长度不合法")
        return
    }

    //数据提交
    try {
        const res = await axios.post("/login", {
            username,
            password
        })
        // console.log(res);
        showToast(res.data.message)

        //缓存数据
        localStorage.setItem("username", res.data.username)
        localStorage.setItem("token", res.data.token)

        //去主页
        setTimeout(() => {
            window.location.href = "./index.html"
        }, 1500)

    } 
    catch (error) {
        showToast(error.response.data.message)
    }
})